package com.guigu.qingcheng.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.guigu.qingcheng.mapper.TbSpecMapper;
import com.guigu.qingcheng.mapper.TbTemplateMapper;
import com.guigu.qingcheng.pojo.TbSpec;
import com.guigu.qingcheng.pojo.TbTemplate;
import com.guigu.qingcheng.pojo.vo.ParaVo;
import com.guigu.qingcheng.service.TbSpecService;
import com.guigu.qingcheng.utils.PageUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;


/**
*
* @author Jiang
* @date 2020/07/25
*/
@Service
public class TbSpecServiceImpl implements TbSpecService {
    @Autowired(required = false)
    private TbSpecMapper tbSpecMapper;

    @Autowired(required = false)
    private TbTemplateMapper tbTemplateMapper;



    @Override
    public List<TbSpec> queryAll() {
        List<TbSpec> tbSpec = tbSpecMapper.selectAll();
            return tbSpec;
    }

    @Override
    public PageUtil<TbSpec> findListAll(Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        Page<TbSpec> pages = (Page<TbSpec>) tbSpecMapper.selectAll();
        PageUtil<TbSpec> pageUtil=new PageUtil<>();
        pageUtil.setRows(pages.getResult());
        pageUtil.setTotal(pages.getTotal());
        return pageUtil;
    }


    @Override
    public PageUtil<TbSpec> pageQueryByAll(Integer pageNum, Integer pageSize, TbSpec tbSpec) {

        Example example=new Example(TbSpec.class);
        Example.Criteria criteria = example.createCriteria();
        // TODO 待实现 重新实现判断参数

        if(!StringUtils.isEmpty(tbSpec.getId())){
            criteria.andEqualTo("id",tbSpec.getId());
        }
        if(!StringUtils.isEmpty(tbSpec.getName())){
           criteria.andLike("name","%"+tbSpec.getName()+"%");
        }
        criteria.andEqualTo("templateId", tbSpec.getTemplateId());
        PageHelper.startPage(pageNum,pageSize);
        Page<TbSpec> pages = (Page<TbSpec>) tbSpecMapper.selectByExample(example);
        PageUtil<TbSpec> pageUtil=new PageUtil<>();
        pageUtil.setRows(pages.getResult());
        pageUtil.setTotal(pages.getTotal());
        return pageUtil;
     }


    @Transactional
    @Override
    public boolean saveTbSpec(TbSpec tbSpec) {
        TbTemplate tbTemplate = tbTemplateMapper.selectByPrimaryKey(tbSpec.getTemplateId());
        tbTemplate.setSpecNum(tbTemplate.getSpecNum()+1);
        tbTemplateMapper.updateByPrimaryKeySelective(tbTemplate);
        int insert = tbSpecMapper.insert(tbSpec);
        if(insert>0){
            return true;
        }
        return false;
    }

    @Override
    public boolean updateTbSpec(TbSpec tbSpec) {
        int i = tbSpecMapper.updateByPrimaryKeySelective(tbSpec);
        if(i>0){
            return true;
        }
        return false;
    }


    @Transactional
    @Override
    public boolean deleteById(Integer id) {
        TbSpec tbSpec = tbSpecMapper.selectByPrimaryKey(id);
        TbTemplate tbTemplate = tbTemplateMapper.selectByPrimaryKey(tbSpec.getTemplateId());
        tbTemplate.setSpecNum(tbTemplate.getSpecNum()-1);
        tbTemplateMapper.updateByPrimaryKeySelective(tbTemplate);
        int i = tbSpecMapper.deleteByPrimaryKey(id);
        if(i>0){
            return true;
        }
        return false;
    }

    @Override
    public TbSpec findById(Integer id) {
        TbSpec tbSpec = tbSpecMapper.selectByPrimaryKey(id);
        return tbSpec;
    }

    @Override
    public PageUtil<ParaVo> specPageQueryByAll(Integer pageNum, Integer pageSize, TbSpec tbSpec) {
        List<ParaVo> paraVos = tbSpecMapper.ParaPageQueryByAll(pageNum-1, pageSize, tbSpec);
        Long total = tbSpecMapper.ParaPageQueryByAlltotal(tbSpec);
        PageUtil<ParaVo> pageUtil = new PageUtil<>();
        pageUtil.setRows(paraVos);
        pageUtil.setTotal(total);
        return pageUtil;
    }

    @Override
    public List<TbSpec> selectSpecByTemplateId(Integer templateId) {
        Example example = new Example(TbSpec.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("templateId", templateId);
        List<TbSpec> tbSpecs = tbSpecMapper.selectByExample(example);
        return tbSpecs;
    }

    @Override
    public List<Map> queryListByCategoryName(String name) {
        return tbSpecMapper.findListByCategoryName(name);
    }
}
